package com.emip.servlet.site;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.emip.bean.AreaBean;
import com.emip.bean.HourStoreBean;
import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.bean.RealDataBean;
import com.emip.bean.SecondStoreBean;
import com.emip.bean.SiteBean;
import com.emip.bean.ValleyBean;
import com.emip.config.SysParams;
import com.emip.pojo.Report;
import com.emip.pojo.Table;
import com.emip.service.AreaService;
import com.emip.service.HourStoreService;
import com.emip.service.ItemService;
import com.emip.service.MonitorTypeService;
import com.emip.service.RealDataService;
import com.emip.service.SecondStoreService;
import com.emip.service.SiteService;
import com.emip.service.ValleyService;
import com.emip.util.ReportFormat;
import com.emip.util.TableFormat;

@Controller
@RequestMapping(value="/site/site")
public class SiteAction {
	
	@Autowired
	SiteService siteService;
	
	@Autowired
	MonitorTypeService monitorTypeService;
	
	@Autowired
	AreaService areaService;
	
	@Autowired
	ValleyService valleyService;
	
	@Autowired
	ItemService itemService;
	
	@Autowired
	RealDataService realDataService;
	
	@Autowired
	SecondStoreService secondService;
	
	@Autowired
	HourStoreService hourService;
	
	@RequestMapping(value="/detail")
	public ModelAndView index(@RequestParam("siteid") String siteid)
	{		
		//ModelAndView mav = new ModelAndView();
		//Map map = new HashMap();
		ModelMap mmap = new ModelMap();  

		//map.put("sitename", siteName);
		SiteBean site = siteService.getSiteByID(siteid);
		mmap.addAttribute("site", site);
		//mav.setViewName("/site/SiteDetail");
		//mav.addAllObjects(map);
		//return mav;
		return new ModelAndView("/site/SiteDetail", mmap);
	}
	
	/**
	@RequestMapping(value="/detailvalue")
	public @ResponseBody RealDataBean getDetailValue(@RequestParam("siteID") String siteID, @RequestParam("itemID") String itemID) 
	{
		SiteBean sBean = siteService.getSiteByID(siteID);
		ItemBean iBean = itemService.getItemByID(itemID);
		return realDataService.getRealDataBySiteItem(sBean, iBean);
	}
	**/
	@RequestMapping(value="/detailvalue")
	public @ResponseBody String getDetailValue(@RequestParam("siteID") String siteID, @RequestParam("itemID") String itemID) 
	{
		SiteBean sBean = siteService.getSiteByID(siteID);
		ItemBean iBean = itemService.getItemByID(itemID);
		RealDataBean b = realDataService.getRealDataBySiteItem(sBean, iBean);
		if (b==null) {
			return "0";
		}
		return b.getValue();
	}
	

	@RequestMapping(value="/last24ItemsBySite")
	public @ResponseBody List<SecondStoreBean> getLast24ItemsBySite(
			@RequestParam("siteID") String siteId,
			@RequestParam("itemID") String itemId) {
		return secondService.getLastNItemsBySiteID(siteId, itemId, 24);
	}
	
	@RequestMapping(value="/last24HItemsBySite")
	public @ResponseBody List<Map<String, Object>> getLast24HItemsBySite(
			@RequestParam("siteID") String siteId,
			@RequestParam("itemID") String itemId) {
		ReportFormat reportFormat = new ReportFormat();
		reportFormat.appendBodyData(hourService.getLastNItemsBySiteID(siteId, itemId, 24));
		Report report = reportFormat.getReport();
		return report.getBodys();
	}
	
	@RequestMapping(value="/management")
	public String index()
	{		
		return "/site/SiteManagement";
	}
	
	@RequestMapping(value="/getAllSites")
	public @ResponseBody Table<SiteBean> getAllSites()
	{
		List<SiteBean> result = siteService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<SiteBean>().formTable(totalPages, 1, records, result);
	}
	
	/**
	public @ResponseBody String getAllSites()
	{
		List<SiteBean> result = siteService.getAll();
		String records = String.valueOf(result.size());
		String totalPages = String.valueOf((result.size() / SysParams.gridDefinedRowNumber) + 1);
		String s = "{\"page\":\"1\",\"total\":\""+totalPages+"\",\"records\":\""+records+"\", \"rows\": [ ";
		StringBuilder str = new StringBuilder(s);
		for (SiteBean bean: result) {
			String id = "";
			if (String.valueOf(bean.getId())!=null) {
				id = String.valueOf(bean.getId());
			}
			String mTypeName = "";
			if (bean.getMonitorType()!=null) {
				mTypeName = bean.getMonitorType().getName();
			}
			String areaID = "";
			String areaDesc = "";
			if (bean.getArea()!=null) {
				areaID = bean.getArea().getAreaID();
				areaDesc = bean.getArea().getProvince()+"/"+bean.getArea().getCity()+"/"+bean.getArea().getDistrict();
			}			
			String name = "";
			if (bean.getName()!=null) {
				name = bean.getName();
			}
			String valleyname = "";
			String waterSystem = "";
			String alongSide = "";
			if (bean.getValley()!=null) {
				valleyname = bean.getValley().getName();
				waterSystem = bean.getValley().getWaterSystem();
				alongSide = bean.getValley().getAlongSide();
			}
			str.append("{\"id\":\"").append(id).append("\", \"mTypeName\":\"").append(mTypeName);
			str.append("\", \"areaID\":\"").append(areaID).append("\", \"areaDesc\":\"").append(areaDesc);
			str.append("\", \"name\":\"").append(name).append("\", \"valleyname\":\"").append(valleyname);
			str.append("\", \"waterSystem\":\"").append(waterSystem).append("\", \"alongSide\":\"").append(alongSide);
			str.append("\"},");
		}
		return str.toString().substring(0, str.toString().length()-1) + "]}";

	}
	**/
	
	@RequestMapping(value="/getSiteByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody SiteBean getSiteByID(@RequestParam("eid") String eid)
	{
		SiteBean beans = siteService.getSiteByID(eid);
		return beans;
	}
	
	@RequestMapping(value="/addSite",method=RequestMethod.POST)
	public @ResponseBody SiteBean addSite(@RequestParam("id") String id, @RequestParam("mTypeID") int mTypeID ,
			@RequestParam("areaID") int areaID,@RequestParam("name") String name,@RequestParam("gprsID") String gprsID,
			@RequestParam("valleyID") int valleyID,@RequestParam("qualityName") String qualityName,@RequestParam("qualityLevel") int qualityLevel,
			@RequestParam("contact") String contact,@RequestParam("telephone") String telephone,@RequestParam("mobile") String mobile,
			@RequestParam("eMail") String eMail,@RequestParam("ipAddress") String ipAddress,@RequestParam("port") String port,
			@RequestParam("freqMin") int freqMin,@RequestParam("gpsFixed") String gpsFixed,@RequestParam("gpsLongitude") String gpsLongitude,
			@RequestParam("gpsLatitude") String gpsLatitude,@RequestParam("remark") String remark,
			@RequestParam("longitudeSetoff") float longitudeSetoff, @RequestParam("latitudeSetoff") float latitudeSetoff) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(mTypeID);
		AreaBean aBean = areaService.getAreaByID(areaID);
		ValleyBean vBean = valleyService.getValleyByID(valleyID);
		SiteBean newBean = siteService.addSite(id, mBean, aBean, name, gprsID, vBean, qualityName, 
				qualityLevel, contact, telephone, mobile, eMail, ipAddress, port, freqMin, gpsFixed, gpsLongitude, gpsLatitude, remark, longitudeSetoff, latitudeSetoff);
		return newBean;	
	}
	
	@RequestMapping(value="/updateSite",method=RequestMethod.POST)
	public @ResponseBody SiteBean updateSite(@RequestParam("id") String id, @RequestParam("mTypeID") int mTypeID ,
			@RequestParam("areaID") int areaID,@RequestParam("name") String name,@RequestParam("gprsID") String gprsID,
			@RequestParam("valleyID") int valleyID,@RequestParam("qualityName") String qualityName,@RequestParam("qualityLevel") int qualityLevel,
			@RequestParam("contact") String contact,@RequestParam("telephone") String telephone,@RequestParam("mobile") String mobile,
			@RequestParam("eMail") String eMail,@RequestParam("ipAddress") String ipAddress,@RequestParam("port") String port,
			@RequestParam("freqMin") int freqMin,@RequestParam("gpsFixed") String gpsFixed,@RequestParam("gpsLongitude") String gpsLongitude,
			@RequestParam("gpsLatitude") String gpsLatitude,@RequestParam("remark") String remark,
			@RequestParam("longitudeSetoff") float longitudeSetoff, @RequestParam("latitudeSetoff") float latitudeSetoff) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(mTypeID);
		AreaBean aBean = areaService.getAreaByID(areaID);
		ValleyBean vBean = valleyService.getValleyByID(valleyID);
		SiteBean newBean = siteService.updateSite(id, mBean, aBean, name, gprsID, vBean, qualityName, 
				qualityLevel, contact, telephone, mobile, eMail, ipAddress, port, freqMin, gpsFixed, gpsLongitude, gpsLatitude, remark, longitudeSetoff, latitudeSetoff);		
		return newBean;
	}
	
	@RequestMapping(value="/deleteSite",method=RequestMethod.POST)
	public @ResponseBody String deleteQualityType(@RequestParam("eID") String id) {
		siteService.deleteSite(id);
		return "success";
	}
	
	@RequestMapping(value="/getAllSitesObj")
	public @ResponseBody List<SiteBean> getAllSitesObj() {
		List<SiteBean> beans = siteService.getAll();
		return beans;
	}
	
	@RequestMapping(value="/getAllSitesObjOrderByProvince")
	public @ResponseBody List<SiteBean> getAllSitesObjOrderByProvince() {
		List<SiteBean> beans = siteService.getAllOrderByProvince();
		return beans;
	}

}
